package net.towee.client.com.repo.view;

import net.towee.client.com.repo.presenter.RepositoryNodeCreatorPresenter.Display;
import net.towee.client.i18n.ToweeConstants;
import net.towee.client.resources.ClientResource;
import net.towee.client.widgets.SheduledInputBox;
import net.towee.client.widgets.SpanPanel;

import com.google.gwt.event.dom.client.HasChangeHandlers;
import com.google.gwt.event.dom.client.HasClickHandlers;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.FlowPanel;
import com.google.inject.Inject;

public class RepositoryNodeCreatorView extends FlowPanel implements Display {

	private final ToweeConstants constants;
	private final ClientResource resource;
	
	private final SheduledInputBox inputBox = new SheduledInputBox();
	private final Button createButton = new Button();
	private final Button cancelButton = new Button();
	private final SpanPanel statusBox = new SpanPanel();
	
	@Inject
	public RepositoryNodeCreatorView(ToweeConstants constants, ClientResource resource){
		this.constants = constants;
		this.resource = resource;
		
		setStyleName( resource.css().repoNodeCreator() );
	
		createButton.setText( constants.create() );
		cancelButton.setText( constants.cancel() );
		
		statusBox.getElement().getStyle().setProperty("position", "absolute");
		statusBox.getElement().getStyle().setProperty("left", "20px");
		statusBox.getElement().getStyle().setProperty("bottom", "47px");
		statusBox.getElement().getStyle().setProperty("color", "red");
		
		// Create input box
		createInputBox();
		add(statusBox);
		createButton();
	}
	
	private void createButton() {
		createButton.setStyleName( resource.css().lobbyNextStepButton() );
		createButton.getElement().getStyle().setProperty("margin", "30px 0 0 20px");
		
		cancelButton.setStyleName( resource.css().lobbyCloseButton() );
		cancelButton.getElement().getStyle().setProperty("margin", "30px 0 0 20px");
		
		add(cancelButton);
		add(createButton);
	}

	private void createInputBox() {
		SpanPanel panel = new SpanPanel("Введите название каталога");
		add(panel);
		
		FlowPanel inputContainer = new FlowPanel();
		inputContainer.setStyleName( resource.css().roundedInputBox() );
		inputContainer.add(inputBox);
		inputBox.getParent().getElement().getStyle().setProperty("border", "2 px solid #aaa");
		add(inputContainer);
	}

	@Override
	public HasClickHandlers getCreateButton() {
		return createButton;
	}

	@Override
	public HasChangeHandlers getSearchChanger() {
		return inputBox;
	}

	@Override
	public String getNodeName() {
		return inputBox.getValue().trim();
	}

	@Override
	public void nodeExists() {
		statusBox.setText( constants.nodeExists() );
		inputBox.getParent().getElement().getStyle().setProperty("border", "2px solid red");
	}

	@Override
	public void nodeNotExists() {
		statusBox.setText("");
		inputBox.getParent().getElement().getStyle().setProperty("border", "2 px solid #aaa");
	}

	@Override
	public HasClickHandlers getCancelButton() {
		return cancelButton;
	}

}